package be.dch.csv.validator;

import java.util.HashSet;
import java.util.Set;

import be.dch.exceptions.ValidationException;

public class Unique implements CellValidator {
    private transient CellValidator validator;

    protected transient Set<Object> foundElements = new HashSet<Object>();

    public Unique() {
	//
    }

    public Unique(final CellValidator validator) {
	this.validator = validator;
    }

    public void validate(final String input) throws ValidationException {
	if (this.foundElements.contains(input)) {
	    throw new ValidationException("Duplicate entry \"" + input);
	} else {
	    this.foundElements.add(input);
	}
	if (this.validator != null) {
	    this.validator.validate(input);
	}

    }

}
